package com.example.bookapp.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.clothingapp.R;
import com.example.clothingapp.SharePreference;
import com.example.clothingapp.bean.UserBean;
import com.example.clothingapp.network.OkHttpUtils;
import com.example.clothingapp.network.PicassoImageLoader;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class UserLoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText mEtName;
    private EditText mEtPassword;
    private Button mBtnLogin;
    private TextView mTvEnroll;
    private ImageView mIvUserLogin;
    private Context context;
    private String password;
    private String name;
    private Gson gson;
    private UserBean user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_user_login);
        initView();
    }
    private void initView() {

        mEtName = findViewById(R.id.et_userlogin_username);
        mEtPassword = findViewById(R.id.et_userlogin_password);
        mBtnLogin = findViewById(R.id.btn_userlogin_login);
        mTvEnroll =  findViewById(R.id.tv_userlogin_register);
        mBtnLogin.setOnClickListener(this);
        mTvEnroll.setOnClickListener(this);
        context = UserLoginActivity.this;
        gson=new Gson();

        mIvUserLogin = findViewById(R.id.iv_image_userlogin);
        //加载图片并显示
        PicassoImageLoader.Load(UserLoginActivity.this,"http://192.168.0.103:1010/clotingserver/clothingimage/loginbackground.jpg",mIvUserLogin);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_userlogin_login:
                if (submit()){
                    //得到输入的数据并使用JSONObject转换为字符串
                    JSONObject jsonObject=new JSONObject();
                    try {
                        jsonObject.put("name",name);
                        jsonObject.put("password",password);
                        //使用okhttp网络请求
                        OkHttpUtils okhttpUtils =new OkHttpUtils();
                        Call call  = okhttpUtils.OKPOST("http://192.168.0.103:1010/clothingserver/user/login",jsonObject.toString());
                        call.enqueue(new Callback() {
                            @Override
                            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                            }
                            @Override
                            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                //封装数据
                                user = gson.fromJson(response.body().string(),UserBean.class);
                                if (user.isSuccess()){
                                    //储存User信息
                                    SharePreference.saveUser(user,context);
                                    finish();
                                    //跳转到注册页面（暂时）
                                    Intent intent=new Intent(UserLoginActivity.this, UserRegisterActivity.class);
                                    startActivity(intent);
                                }else {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(UserLoginActivity.this, "输入的账号或密码有误", Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                }
                            }
                        });

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case R.id.tv_userlogin_register:
                //实现注册页面的跳转
                Intent intent=new Intent(UserLoginActivity.this, UserRegisterActivity.class);
                startActivity(intent);
                break;
        }
    }
    private Boolean submit() {
        // 确认是否已经输入账号和密码
        name = mEtName.getText().toString().trim();
        password = mEtPassword.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;

    }
}